package com.nulldev.util.os.notifications;

import java.io.IOException;

import com.nulldev.util.java.ProcessUtil;
import com.nulldev.util.java.ProcessUtil.ProcessUtilRead;
import com.nulldev.util.os.OSUtil;
import com.nulldev.util.os.OSUtil.OSType;

public class mExecutor {
	
	public static void writeNotification(final String title, final String message) {
		writeNotification(title, message, MessagePriority.NORMAL);
	}
	
	public static void writeNotification(final String title, final String message, final MessagePriority priority) {
		if (OSUtil.getOperatingSystemType() == OSType.Windows) {
			
		}
		if (OSUtil.getOperatingSystemType() == OSType.Linux) {
			if (OSUtil.PackageUtil.isPackage("notify-send")) {
				String test = "";
				try {
					test = ProcessUtil.execCmd("notify-send -v");
				} catch (IOException e) {
				}
				if (test.startsWith("notify-send ")) {
					if (priority == MessagePriority.CRITICAL) {
						try {
							final ProcessBuilder pb = new ProcessBuilder("notify-send", "-u", "critical", title, message);
							ProcessUtil.ProcessUtilActive.execActiveCmd_NoQueue_UseProcessBuilder(pb, ProcessUtilRead.NULL);
							return;
						}catch (Exception e) {
							e.printStackTrace();
						}
					}else if (priority == MessagePriority.WARNING) {
						try {
							final ProcessBuilder pb = new ProcessBuilder("notify-send", "-u", "critical", title, message);
							ProcessUtil.ProcessUtilActive.execActiveCmd_NoQueue_UseProcessBuilder(pb, ProcessUtilRead.NULL);
							return;
						}catch (Exception e) {
							e.printStackTrace();
						}
					}else if (priority == MessagePriority.NORMAL) {
						try {
							final ProcessBuilder pb = new ProcessBuilder("notify-send", "-u", "normal", title, message);
							ProcessUtil.ProcessUtilActive.execActiveCmd_NoQueue_UseProcessBuilder(pb, ProcessUtilRead.NULL);
							return;
						}catch (Exception e) {
							e.printStackTrace();
						}
					}else {
						try {
							final ProcessBuilder pb = new ProcessBuilder("notify-send", "-u", "low", title, message);
							ProcessUtil.ProcessUtilActive.execActiveCmd_NoQueue_UseProcessBuilder(pb, ProcessUtilRead.NULL);
							return;
						}catch (Exception e) {
							e.printStackTrace();
						}
					}
				}
			}else if (OSUtil.PackageUtil.isPackage("twmnc")) {
				try {
					final ProcessBuilder pb = new ProcessBuilder("twmnc", "-t", title, "-c", message);
					ProcessUtil.ProcessUtilActive.execActiveCmd_NoQueue_UseProcessBuilder(pb, ProcessUtilRead.NULL);
					return;
				}catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		OSUtil.displayNotification(title, message, priority);
	}
	
	public static enum MessagePriority {
		/**
		 * Windows: Error Icon is displayed. <br>
		 * Linux: Depends on your notification handler. <br>
		 * MacOS: N/A [Depends on JVM.]
		 */
		CRITICAL, 
		/**
		 * Windows: Warning Icon is displayed. <br>
		 * Linux: Depends on your notification handler. <br>
		 * MacOS: N/A [Depends on JVM.]
		 */
		WARNING,
		/**
		 * Windows: Normal Icon is displayed. <br>
		 * Linux: Depends on your notification handler. <br>
		 * MacOS: N/A [Depends on JVM.]
		 */
		NORMAL,
		/**
		 * Windows: Linux specific, same as NORMAL. <br>
		 * Linux: Depends on your notification handler. <br>
		 * MacOS: N/A [Depends on JVM.]
		 */
		LOW,
		;
	}
}
